/*
 *所有关于Warehouse类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*WarehouseService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MyMES.IRepositories;
using MyMes.Infrastructure;
using MyMES.Infrastructure;
using Azure.Core;
using MyMES.Repositories;
using MyMES.Dto;

namespace MyMES.Services
{
    public partial class WarehouseService
    {
        /// <summary>
        /// 仓库设置仓储
        /// </summary>
        private readonly IBaseRepository<Warehouse> _warehouseRepository;
        /// <summary>
        /// 库区仓储
        /// </summary>
        private readonly IBaseRepository<ReservoirArea> _reservoirAreaRepository;
        /// <summary>
        /// 库位仓储
        /// </summary>
        private readonly IBaseRepository<BinLocation> _binLocationRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="warehouseRepository">仓库设置仓储</param>
        /// <param name="reservoirAreaRepository">库区仓储</param>
        /// <param name="binLocationRepository">库位仓储</param>
        public WarehouseService(IBaseRepository<Warehouse> warehouseRepository, IBaseRepository<ReservoirArea> reservoirAreaRepository,IBaseRepository<BinLocation> binLocationRepository)
        {
            _warehouseRepository = warehouseRepository;
            _reservoirAreaRepository = reservoirAreaRepository;
            _binLocationRepository = binLocationRepository;
        }

        #region 仓库设置

        /// <summary>
        /// 获取仓库设置信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<APIPage<Warehouse>> GetWarehousePageList(GeiWarehousePageListDTO request)
        {
            var list = _warehouseRepository.GetAll();

            if (!string.IsNullOrEmpty(request.WarehouseCode))
            {
                list = list.Where(e => e.WarehouseCode.Contains(request.WarehouseCode));
            }
            if (!string.IsNullOrEmpty(request.WarehouseName))
            {
                list = list.Where(e => e.WarehouseName.Contains(request.WarehouseName));
            }

            var supplierpaging = new APIPage<Warehouse>()
            {
                Data = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList(),
                TotalCount = list.Count(),
                TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize)
            };

            return supplierpaging;
        }

        /// <summary>
        /// 新增仓库设置信息
        /// </summary>
        /// <param name="param">供货商参数</param>
        /// <returns></returns>
        public async Task<int> CreateWarehouse(Warehouse param)
        {
            var result = await _warehouseRepository.AddAsync(param);

            return result;
        }

        /// <summary>
        /// 删除仓库设置信息
        /// </summary>
        /// <param name="param">供货商参数</param>
        /// <returns></returns>
        public async Task<int> DeleteWarehouse(Warehouse param)
        {
            var result = await _warehouseRepository.Remove(param);

            return result;
        }

        /// <summary>
        /// 修改仓库设置信息
        /// </summary>
        /// <param name="param">供货商参数</param>
        /// <returns></returns>
        public async Task<int> UpdateWarehouse(Warehouse param)
        {
            var result = await _warehouseRepository.UpdateAsync(param);

            return result;
        }

        #endregion


        #region 库区表

        /// <summary>
        /// 获取库区信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<APIPage<ReservoirArea>> GetReservoirAreaPageList(GetReservoirAreaPageListDTO request)
        {
            var list = _reservoirAreaRepository.GetAll().Where(e=>e.WarehouseCode == request.WarehouseCode);

            if (!string.IsNullOrEmpty(request.ReservoirAreaName))
            {
                list = list.Where(e => e.ReservoirAreaName.Contains(request.ReservoirAreaName));
            }

            var supplierpaging = new APIPage<ReservoirArea>()
            {
                Data = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList(),
                TotalCount = list.Count(),
                TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize)
            };

            return supplierpaging;
        }

        /// <summary>
        /// 新增库区信息
        /// </summary>
        /// <param name="param">供货商参数</param>
        /// <returns></returns>
        public async Task<int> CreateReservoirArea(ReservoirArea param)
        {
            var result = await _reservoirAreaRepository.AddAsync(param);

            return result;
        }

        /// <summary>
        /// 删除库区信息
        /// </summary>
        /// <param name="param">供货商参数</param>
        /// <returns></returns>
        public async Task<int> DeleteReservoirArea(ReservoirArea param)
        {
            var result = await _reservoirAreaRepository.Remove(param);

            return result;
        }

        /// <summary>
        /// 修改库区信息
        /// </summary>
        /// <param name="param">供货商参数</param>
        /// <returns></returns>
        public async Task<int> UpdateReservoirArea(ReservoirArea param)
        {
            var result = await _reservoirAreaRepository.UpdateAsync(param);

            return result;
        }

        #endregion


        #region 库位表

        /// <summary>
        /// 获取库位信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<APIPage<BinLocation>> GetBinLocationPageList(GetBinLocationPageListDTO request)
        {
            var list = _binLocationRepository.GetAll().Where(e => e.ReservoirAreaCode == request.ReservoirAreaCode);

            if (!string.IsNullOrEmpty(request.BinLocationCode))
            {
                list = list.Where(e => e.BinLocationCode.Contains(request.BinLocationCode));
            }

            if (!string.IsNullOrEmpty(request.BinLocationName))
            {
                list = list.Where(e => e.BinLocationName.Contains(request.BinLocationName));
            }

            if (request.LocationX > 0)
            {
                list = list.Where(e => e.LocationX == request.LocationX);
            }

            if (request.LocationY > 0)
            {
                list = list.Where(e => e.LocationY == request.LocationY);
            }

            if (request.LocationZ > 0)
            {
                list = list.Where(e => e.LocationZ == request.LocationZ);
            }

            var supplierpaging = new APIPage<BinLocation>()
            {
                Data = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList(),
                TotalCount = list.Count(),
                TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize)
            };

            return supplierpaging;
        }

        /// <summary>
        /// 新增库位信息
        /// </summary>
        /// <param name="param">库位参数</param>
        /// <returns></returns>
        public async Task<int> CreateBinLocation(BinLocation param)
        {
            var result = await _binLocationRepository.AddAsync(param);

            return result;
        }

        /// <summary>
        /// 删除库位信息
        /// </summary>
        /// <param name="param">库位参数</param>
        /// <returns></returns>
        public async Task<int> DeleteBinLocation(BinLocation param)
        {
            var result = await _binLocationRepository.Remove(param);

            return result;
        }

        /// <summary>
        /// 修改库位信息
        /// </summary>
        /// <param name="param">库位数</param>
        /// <returns></returns>
        public async Task<int> UpdateBinLocation(BinLocation param)
        {
            var result = await _binLocationRepository.UpdateAsync(param);

            return result;
        }

        #endregion

    }
}
